package com.hdu.dwh.service.impl;

import com.hdu.dwh.service.*;
import com.hdu.dwh.vos.*;
import com.hdu.dwh.constants.CommonConstants;
import com.hdu.dwh.constants.ParamConstants;
import com.hdu.dwh.results.ResponseResult;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@RequiredArgsConstructor
public class AssetServiceImpl implements AssetService {

    private final DataWarehouseDatabaseService dwhDatabaseService;
    private final DataWarehouseTableService dwhTableService;
    private final ISubjectDatabaseService subjectDatabaseService;
    private final ISubjectTableService subjectTableService;


    /**
     * 资产概览
     * @return
     */
    @Override
    public ResponseResult overview() {
        // 数据库数量
        long sourceTotalDatabases = dwhDatabaseService.count();
        long subjectTotalDatabases = subjectDatabaseService.count();
        // 数据表数量 结构化 非结构化 主题数据库按全结构化算
        long sourceStructuredTables = dwhTableService.countTablesByDataType(ParamConstants.STRUCTURED);
        long sourceUnstructuredTables = dwhTableService.countTablesByDataType(ParamConstants.NON_STRUCTURED);
        long subjectTotalTables = subjectTableService.count();
        // 数据表数量 按层级
        long sourceODS = dwhTableService.countTablesByHierarchy(ParamConstants.ODS);
        long subjectODS = subjectTableService.countTablesByHierarchy(ParamConstants.ODS);
        long sourceDWD = dwhTableService.countTablesByHierarchy(ParamConstants.DWD);
        long subjectDWD = subjectTableService.countTablesByHierarchy(ParamConstants.DWD);
        long sourceDWS = dwhTableService.countTablesByHierarchy(ParamConstants.DWS);
        long subjectDWS = subjectTableService.countTablesByHierarchy(ParamConstants.DWS);
        long sourceADS = dwhTableService.countTablesByHierarchy(ParamConstants.ADS);
        long subjectADS = subjectTableService.countTablesByHierarchy(ParamConstants.ADS);
        long sourceDIM = dwhTableService.countTablesByHierarchy(ParamConstants.DIM);
        long subjectDIM = subjectTableService.countTablesByHierarchy(ParamConstants.DIM);
        // 构造返回
        AssetOverviewVo assetOverviewVo = new AssetOverviewVo()
                .setTotalDatabases(sourceTotalDatabases + subjectTotalDatabases)
                .setTotalTables(sourceStructuredTables + sourceUnstructuredTables + subjectTotalTables)
                .setStructuredTables(sourceStructuredTables + subjectTotalTables)
                .setUnstructuredTables(sourceUnstructuredTables)
                .setODS(sourceODS + subjectODS)
                .setDWD(sourceDWD + subjectDWD)
                .setDWS(sourceDWS + subjectDWS)
                .setADS(sourceADS + subjectADS)
                .setDIM(sourceDIM + subjectDIM);
        return ResponseResult.success(assetOverviewVo);
    }


    /**
     * 资产目录树
     * @return
     */
    @Override
    public ResponseResult tree() {
        TreeVo sourceTreeVo = dwhTableService.tree();
        TreeVo subjectTreeVo = subjectTableService.getTreeVo();
        List<TreeVo> treeVos = new ArrayList<>();
        if (Objects.nonNull(sourceTreeVo)) {
            treeVos.add(sourceTreeVo);
        }
        if (Objects.nonNull(subjectTreeVo)) {
            treeVos.add(subjectTreeVo);
        }
        if(treeVos.isEmpty()){
            return ResponseResult.fail(CommonConstants.RECORD_NOT_EXIST);
        }
        return ResponseResult.success(treeVos);
    }
}
